package com.xd.core.bill.controller;

import java.util.concurrent.atomic.AtomicInteger;

public class Dome {
   static AtomicInteger count = new AtomicInteger();
    public static void main(String[] args) {
        BlockQueueDome<Integer> q = new BlockQueueDome<>(30);
        Thread[] product = new Thread[3];
        Thread[] consumer = new Thread[1];

        for (int i = 0; i < product.length; i++) {
            product[i] = new Thread(()->{
                while (true){
                    try {
                        int d = count.getAndIncrement();
                        q.put(d);
                        System.out.println("生产者："+Thread.currentThread().getName()+"生产了："+d);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            product[i].setName(String.format("生产者%d",i));
        }

        for (int i = 0; i < consumer.length; i++) {
            consumer[i] = new Thread(()->{
                while (true){
                    try {
                        Integer d = q.take();
                        System.out.println("消费者："+Thread.currentThread().getName()+"消费了："+d);
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            consumer[i].setName(String.format("消费者%d",i));
        }
        for (Thread value : product) {
            value.start();
        }
        for (Thread thread : consumer) {
            thread.start();
        }
    }
}
